from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QLabel
from qfluentwidgets import SettingCard, FluentIconBase
from qfluentwidgets  import Slider
from app.common.config import cfg

class SliderSettingCard(SettingCard):
    """ 滑块设置卡片 """
    
    valueChanged = Signal(int)

    def __init__(self, icon: FluentIconBase, title: str, content: str, 
                 configItem, parent=None, minimum=0, maximum=100):
        """
        Parameters
        ----------
        icon: FluentIconBase
            卡片图标
        title: str
            卡片标题
        content: str
            卡片描述文本
        configItem: ConfigItem
            配置项
        parent: QWidget
            父级窗口
        minimum: int
            滑块最小值
        maximum: int
            滑块最大值
        """
        super().__init__(icon, title, content, parent)
        self.configItem = configItem
        
        # 创建 QLabel 用于显示值
        self.valueLabel = QLabel(str(self.configItem.value), self)
        self.valueLabel.setMinimumWidth(30)
        self.valueLabel.setAlignment(Qt.AlignRight | Qt.AlignVCenter)

        # 创建滑块控件
        self.slider = Slider(Qt.Horizontal, self)
        self.slider.setMinimum(minimum)
        self.slider.setMaximum(maximum)
        self.slider.setValue(self.configItem.value)
        self.slider.setMinimumWidth(200)
        
        # 将 QLabel 和滑块添加到右侧
        self.hBoxLayout.addWidget(self.valueLabel, 0, Qt.AlignRight)
        self.hBoxLayout.addWidget(self.slider, 0, Qt.AlignRight)
        self.slider.valueChanged.connect(self._onValueChanged)

    def _onValueChanged(self, value: int):
        """ 值改变时的处理 """
        self.configItem.value = value
        cfg.save()
        self.valueLabel.setText(str(value))
        self.valueChanged.emit(value)

    def getValue(self) -> int:
        """ 获取当前值 """
        return self.slider.value()

    def setValue(self, value: int):
        """ 设置值 """
        self.slider.setValue(value)
        self.valueLabel.setText(str(value)) 